'use strict';

const path = require('path');
const { Controller } = require('ee-core');
const Log = require('ee-core/log');
const Services = require('ee-core/services');
const CoreElectronWindow = require('ee-core/electron/window');
const CoreElectronApp = require('ee-core/electron/app');
const Ps = require('ee-core/ps');
const Conf = require('ee-core/config')
const { Menu, BrowserWindow } = require('electron');
const homeController = require('./home')

/**
 * example
 * @class
 */
class ExampleController extends Controller {

  constructor(ctx) {
    super(ctx);
  }


  /**
   * 所有方法接收两个参数
   * @param args 前端传的参数
   * @param event - ipc通信时才有值。详情见：控制器文档
   */

  /**
   * test
   */
  async test () {
    const result = await Services.get('example').test('electron');
    Log.info('service result:', result);

    return 'hello electron-egg';
  }

  /**
   * 打开登录页
   */
  async loginWindowOpen () {
    Log.info('Login Window Open')
    const mainWindow = CoreElectronWindow.getMainWindow();
    mainWindow.setTitle('顺丰进销存-登录')
    mainWindow.setResizable(true)
    mainWindow.setSize(420, 380, true)
    mainWindow.setResizable(false)
    mainWindow.setMenuBarVisibility(false)
    return ''
  }

  /**
   * 打开首页
   */
  async mainWindowOpen () {
    Log.info('mainWindowOpen');
    Log.info('CoreApp', CoreElectronApp)
    const mainWindow = CoreElectronWindow.getMainWindow();
    mainWindow.setTitle('顺丰进销存-首页')
    mainWindow.setSize(900, 620)
    mainWindow.setResizable(false)
    mainWindow.setMenuBarVisibility(true)
    this.setHomeMenu()
    return 'Hello'
  }

  setHomeMenu () {
    const menuTemplate = [
      {
        label: '系统',
        submenu: [
          {
            label: 'New',
            click: () => {
              // 处理点击事件
            }
          },
          {
            label: '用户管理',
            click: () => {
              // 处理点击事件
              const child = new BrowserWindow({
                parent: true,
                title: '用户管理',
                autoHideMenuBar: true
              });
              let addr = 'http://localhost:8080'
              let contentUrl = null;
              if (Ps.isProd()) {
                const mainServer = Conf.getValue('mainServer');
                if (Conf.isFileProtocol(mainServer)) {
                  addr = mainServer.protocol + path.join(Ps.getHomeDir(), mainServer.indexPath);
                } else {
                  addr = mainServer.protocol + mainServer.host + ':' + mainServer.port;
                }
              }

              contentUrl = addr + '#/userManage';

              Log.info('Final Address: ', contentUrl)
              child.loadURL(contentUrl)
              child.once('ready-to-show', () => {
                child.show()
              })
            }
          },
          {
            type: 'separator'
          },
          {
            label: '退出登录',
            click: () => {
              const mainWindow = CoreElectronWindow.getMainWindow();
              mainWindow.webContents.send('controller.global.quitLogin')
            }
          }
        ]
      },
      {
        label: '关于',
        submenu: [
          {
            label: '关于软件',
            async click() {
              await Services.get('home').openWindow({
                content: '#/about',
                windowName: 'AboutUs',
                windowTitle: '关于我们'
              })
            }
          }
        ]
      }
    ];

    // 根据菜单模板创建菜单
    const menu = Menu.buildFromTemplate(menuTemplate);

    // 将菜单应用到窗口
    Menu.setApplicationMenu(menu);
  }
}

ExampleController.toString = () => '[class ExampleController]';
module.exports = ExampleController;
